package com.canbot.u05.api;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import com.canbot.u05.receivers.IntentKey;
import com.canbot.u05.receivers.ReceiverSocketAction;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;


public class U05RobotManger {

        private static final String TAG = "U05RobotManger";

        private static U05RobotManger one = new U05RobotManger();

        private BroadcastReceiver wkReceiver;

        private BroadcastReceiver mVoiceResultEventReceiver;

        //    private BroadcastReceiver mBackToWakeUpReceiver;
        private BroadcastReceiver mVoiceControlReceiver;

        private U05RobotManger() {

        }

        public static U05RobotManger getInstance() {
                return one;
        }

        /**
         * 播放rk3288平板上的音频，路径是sd卡根开始的路径.
         *
         * @param path
         */
        public final void playSound(String path) {
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, path);
        }

        /**
         * wifi 连接 目前没有返回值，不知道是否执行成功.
         *
         * @param ssid
         * @param password
         */
        public void connectToWIFI(String ssid, String password) {
                MsgSendUtils.sendStringMsg(MsgType.WIFI_PASSWORD,
                        ssid + "&&" + password);
        }

        /**
         * 开始语音识别
         */
        public void startVoiceRecognition() {
                MsgSendUtils.sendStringMsg(MsgType.START_RECOGNIZER, "ok");//
        }

        /**
         * 停止语音识别
         */
        public void stopVoiceRecognition() {
                MsgSendUtils.sendStringMsg(MsgType.STOP_RECOGNIZER, "ok");
        }

        public void registerWakeUpReceiver(Context context,
                                           WakeUpReceiver receiver) {
                final WakeUpReceiver lo = receiver;
                wkReceiver = new BroadcastReceiver() {
                        public void onReceive(Context context, Intent intent) {
                                lo.onHandleWakeUpEvent();
                        }
                };
                IntentFilter filter = new IntentFilter();
                filter.addAction(ReceiverSocketAction.MSG_VOICE_RECOGNIZER_WAKEUP);
                LocalBroadcastManager.getInstance(context).registerReceiver(wkReceiver,
                        filter);
        }


        public void unRegisterWakeUpReceiver(Context context) {
                LocalBroadcastManager.getInstance(context)
                        .unregisterReceiver(wkReceiver);
        }

        public void registerVoiceResultReceiver(Context context,
                                                VoiceRecognitionResultReceiver receiver) {
                final VoiceRecognitionResultReceiver lo = receiver;
                mVoiceResultEventReceiver = new BroadcastReceiver() {
                        public void onReceive(Context context, Intent intent) {
                                String result = intent.getStringExtra(IntentKey.VOICE_RESULT);
                                lo.onHandleVoiceResulEvent(context, result);
                        }
                };
                IntentFilter filter = new IntentFilter();
                filter.addAction(ReceiverSocketAction.MSG_VOICE_RECOGNIZER_RESULT);
                context.registerReceiver(mVoiceResultEventReceiver, filter);
        }

        public void unRegisterVoiceResultReceiver(Context context) {
                context.unregisterReceiver(mVoiceResultEventReceiver);
        }

        /**
         * 声控界面状态交互
         *
         * @param context
         * @param receiver
         */
        public void registerChattingReceiver(Context context,
                                             ChattingReceiver receiver) {
                final ChattingReceiver lo = receiver;
                mVoiceControlReceiver = new BroadcastReceiver() {
                        public void onReceive(Context context, Intent intent) {
                                if (intent != null) {
                                        String action = intent.getAction();
                                        if (action != null && !action.isEmpty()) {
                                                if (action.equals(
                                                        ReceiverSocketAction.RECEIVER_MSG_START_RECODER)) {
                                                        lo.onHandleRecoderEvent(context);
                                                }
                                                else if (action.equals(
                                                        ReceiverSocketAction.RECEIVER_MSG_START_RECOGNIZER)) {
                                                        lo.onHandleRecognizerEvent(context);
                                                }
                                                else if (action.equals(
                                                        ReceiverSocketAction.RECEIVER_MSG_SUSPEND)) {
                                                        Logger.e(TAG, "RECEIVER_MSG_SUSPEND: " + " packegerNanme :" + context.getPackageName());
                                                        Logger.e(TAG, "RECEIVER_MSG_SUSPEND: " + " contextNanme :" + context.getClass().getName());
                                                        lo.onHandleEvent(context);
                                                }
                                        }
                                }

                        }
                };
                IntentFilter filter = new IntentFilter();
                filter.addAction(ReceiverSocketAction.RECEIVER_MSG_START_RECODER);
                filter.addAction(ReceiverSocketAction.RECEIVER_MSG_SUSPEND);
                filter.addAction(ReceiverSocketAction.RECEIVER_MSG_START_RECOGNIZER);
                LocalBroadcastManager.getInstance(context)
                        .registerReceiver(mVoiceControlReceiver, filter);
        }

        public void unRegisterChattingReceiver(Context context) {
                LocalBroadcastManager.getInstance(context)
                        .unregisterReceiver(mVoiceControlReceiver);
        }

        public static interface WakeUpReceiver {

                public void onHandleWakeUpEvent();
        }

        ;


        public static interface VoiceRecognitionResultReceiver {

                public void onHandleVoiceResulEvent(Context context, String result);
        }

        ;

        public static interface ChattingReceiver {

                public void onHandleRecoderEvent(Context context);

                public void onHandleEvent(Context context);

                public void onHandleRecognizerEvent(Context context);
        }

        ;

}
